package com.stedu.mall.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String username, String password) throws SteduException {
        //根据用户名查询判断用户是否存在
        User user = userMapper.selectByUsername(username);
        if(user == null){
            throw  new SteduException("该用户不存在，无法登录");
        }
        //判断用户是否禁用或注销
        if(user.getStatus().equals(2) || user.getStatus().equals(3)){
            throw new SteduException("该用户被禁用或注销，无法登录");
        }
        //对输入的密码加密
        String md5Pwd =SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        //和数据库中的密码对比
        if(!md5Pwd.equals((user.getPassword()))){
            throw new SteduException("密码输入错误，无法登录");
        }
        return user;
    }

    @Override
    public boolean reg(User user) throws SteduException {
        //判断该用户在系统中是否存在
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            throw new SteduException("该用户已存在，无法注册~");
        }
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);
        //对密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword()+salt));
        user.setPassword(md5Pwd);
        //添加用户
        return userMapper.insert(user) == 1;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }
    @Override
    public boolean insert(User user) {
        String salt = RandomUtil.randomString(7);
        String password = SecureUtil.md5(SecureUtil.md5("123"+salt));
        user.setPassword(password);
        String payPassword = SecureUtil.md5(SecureUtil.md5("123"+salt));
        user.setPayPassword(payPassword);
        user.setSalt(salt);
        user.setStatus(0);
        user.setRegTime(new Timestamp(System.currentTimeMillis()));
        user.setMoney(new BigDecimal(0));
        return userMapper.insert(user)==1;
    }
    @Override
    public boolean delete(Integer id) {
        return userMapper.delete(id)==1;
    }
    @Override
    public boolean update(User user) {
        String password = SecureUtil.md5(SecureUtil.md5("123"+user.getSalt()));
        user.setPassword(password);
        return userMapper.update(user)==1;
    }
    @Override
    public boolean updateStatus(User user) {
        return userMapper.update(user)==1;
    }

    @Override
    public boolean updateInfo(User user) {
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean recharge(User user, BigDecimal money) {
        User user1 = userMapper.selectById(user.getId());
        user1.setMoney(user1.getMoney().add(money));
        userMapper.update(user1);
        return userMapper.update(user1) == 1;
    }

    @Override
    public boolean updatePassword(String oldPassword, String newPassword, Integer id) throws SteduException {
        /*
            判断原密码是否正确
            1. 根据id查询用户信息，获取原密码和盐
            2. 使用用户输入的原密码和盐加密
            3. 和数据库中的原密码比较
         */
        //根据id查询用户信息，获取原密码和盐
        User user = userMapper.selectById(id);
        String salt = user.getSalt();
        //使用用户输入的原密码和盐加密
        String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPassword + salt));
        //和数据库中的原密码比较
        if (!user.getPassword().equals(md5InputOldPwd)) {
            throw new SteduException("原密码错误，请重新输入");
        }
        if (oldPassword.equals(newPassword)) {
            throw new SteduException("新密码和原密码相同，请重新输入");
        }
        //对新密码结合盐进行加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPassword + salt));
        //修改密码
        user = new User();
        user.setId(id);
        user.setPassword(md5NewPwd);

        return userMapper.update(user) == 1;
    }

    @Override
    public boolean updatePayPassword1(String oldPayPassword, String newPayPassword, Integer id) throws SteduException {
        if (oldPayPassword != null) {
        /*
            判断原密码是否正确
            1. 根据id查询用户信息，获取原密码和盐
            2. 使用用户输入的原密码和盐加密
            3. 和数据库中的原密码比较
         */
            //根据id查询用户信息，获取原密码和盐
            User user = userMapper.selectById(id);
            String salt = user.getSalt();
            //使用用户输入的原密码和盐加密
            String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPayPassword + salt));
            //和数据库中的原密码比较
            if (!user.getPayPassword().equals(md5InputOldPwd)) {
                throw new SteduException("原密码错误，请重新输入");
            }
            if (oldPayPassword.equals(newPayPassword)) {
                throw new SteduException("新密码和原密码相同，请重新输入");
            }
            //对新密码结合盐进行加密
            String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPayPassword + salt));
            //修改密码
            user = new User();
            user.setId(id);
            user.setPayPassword(md5NewPwd);

            return userMapper.update(user) == 1;
        } else {
            //根据id查询用户信息，获取原密码和盐
            User user = userMapper.selectById(id);
            String salt = user.getSalt();
            //判断原支付密码是否为空
            if (user.getPayPassword() == null) {
                //对新密码结合盐进行加密
                String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPayPassword + salt));
                //修改密码
                user = new User();
                user.setId(id);
                user.setPayPassword(md5NewPwd);
            } else {
                throw new SteduException("原支付密码不为空无法设置");
            }

            return userMapper.update(user) == 1;
        }
    }

    @Override
    public boolean updatePayPassword2(String newPayPassword, Integer id) throws SteduException {
        //根据id查询用户信息，获取原密码和盐
        User user = userMapper.selectById(id);
        String salt = user.getSalt();
        //判断原支付密码是否为空
        if (user.getPayPassword() == null) {
            //对新密码结合盐进行加密
            String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPayPassword + salt));
            //修改密码
            user = new User();
            user.setId(id);
            user.setPayPassword(md5NewPwd);
        } else {
            throw new SteduException("原支付密码不为空无法设置");
        }

        return userMapper.update(user) == 1;
    }

    @Override
    public PageInfo<User> selectAll(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAll(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
}
